<?

//Data loading
$title='JOURNAL TRANSACTION';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI TATANAN CONTAINER';

$page   = 'PAGE';

$DEFINE_FONT=0;
$Result = GridList();
if ($Result) {
	foreach ($Result as $Row) {
		foreach ($Row as $Field) {
			if (strlen($strExcelRow)) $strExcelRow .= "\t$Field";
			else $strExcelRow = "$Field";
		}
		$strOutput .= $strExcelRow."\n";
		$strExcelRow="";
	}
	header("Content-type:application/vnd.notepad");
	header("Content-Disposition:filename=ais_report_journaltrans.csv");
	print $strOutput;
}


function GridList () {
	    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
	    $WHERE  = BuildCondition (GetParam("source",""));

		if (GetParam("office","")) {
	     $WHERE .= " AND ktrasl = '".GetParam("office","")."' ";
    }

    if (GetParam("fromDate","")) {
        list($FDay,$FMonth,$FYear) = explode("/",GetParam("fromDate",""));
	      $WHERE .= " AND tglbkt>='$FYear-$FMonth-$FDay' ";
	  }

	  if (GetParam("toDate","")) {
	     list($TDay,$TMonth,$TYear) = explode("/",GetParam("toDate",""));
	     $WHERE .= " AND tglbkt<='$TYear-$TMonth-$TDay' ";
		}

	    $SQL  = "SELECT nobkt,noref,uraian,skada,debtur,untush,curr,dk,jumlah,rupiah FROM ais_memorial WHERE 1 ".$WHERE;

      $SQL .= " ORDER BY nobkt ";
      $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
      return $MyResult;
} 

function BuildCondition ($no) {
	$row_number = explode(",",$no);
	//print $row_number[0];
	for ($x=0;$x<count($row_number);$x++) {
		if (strlen($WHERE)) $WHERE .= " OR nobkt like '$row_number[$x]%' ";
		else $WHERE = " nobkt like '$row_number[$x]%' ";
	}
	if (strlen($WHERE)) $WHERE = " AND ".$WHERE;
	return $WHERE;
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return $arraymonth[$month].", $year";
}


class GridPDFReport extends PDF {

	function Header() {
		global $title,$perush,$page,$DEFINE_FONT;
		if ($DEFINE_FONT==0) {
			$this->AddFont('arialn','','arialn.php');
			$DEFINE_FONT=1;
		}
		$this->SetFont('arialn','',10);
    	$this->Cell(195,5,$perush,0,1,'C',0);
    	$this->Cell(195,5,$title,0,1,'C',0);
    	$this->Cell(195,5,"PERIOD: ".GetParam("fromDate","")." TO ".GetParam("toDate",""),0,1,'C',0);
    	$this->Cell(195,5,$page." ".$this->PageNo(),0,1,'R',0);
	    //Header
	    //Column widths
	    $w=array(25,25,50,20,20,10,10,20,20);
	    $this->SetFont('arialn','',10);
	    $this->Cell($w[0],7,"VOUCHER #",1,0,'C',0);
	    $this->Cell($w[1],7,"REF #",1,0,'C',0);
	    $this->Cell($w[2],7,"DESCRIPTION",1,0,'C',0);
	    $this->Cell($w[3],7,"SKADA",1,0,'C',0);
	    $this->Cell($w[4],7,"DEBTUR",1,0,'C',0);
	    $this->Cell($w[5],7,"UU",1,0,'C',0);
	    $this->Cell($w[6],7,"CUR",1,0,'C',0);
	    $this->Cell($w[7],7,"DEBET",1,0,'C',0);
	    $this->Cell($w[8],7,"CREDIT",1,0,'C',0);
	    $this->Ln();
	    $this->SetFont('arialn','',8);
	}

	function GridTable($Result) {
	    //Column widths
	    $w=array(25,25,50,20,20,10,10,20,20);
	    //Data
	    foreach($Result as $row)
	    {
		    if ($row["dk"]=="D") $row["debet"] = $row["jumlah"];
			else $row["credit"] = $row["jumlah"];
			
		    $totald = $totald + $row["debet"];
		    $totalc = $totalc + $row["credit"];
		    
	        if (($LastNo<>$row["nobkt"]) && (strlen($LastNo)>0)) {
		        $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5]+$w[6],7,"SUB TOTAL:",1,0,'R',0);
		        $this->Cell($w[7],7,number_format($subtotald,2),1,0,'R',0);
				$this->Cell($w[8],7,number_format($subtotalc,2),1,0,'R',0);
		        $this->Ln();
	        	$subtotald = $row["debet"];
	        	$subtotalc = $row["credit"];
        	} else {
	        	$subtotald = $subtotald + $row["debet"];
	        	$subtotalc = $subtotalc + $row["credit"];
	        }
		    
			
			if (($LastNo<>$row["nobkt"]) && (strlen($row["nobkt"])))
	        	$this->Cell($w[0],6,$row["nobkt"],'LR');
	        else
	         	$this->Cell($w[0],6,"",'LR');

	        $this->Cell($w[1],6,$row["noref"],'LR');
	        $this->Cell($w[2],6,$row["uraian"],'LR');
	        $this->Cell($w[3],6,$row["skada"],'LR');
	        $this->Cell($w[4],6,$row["debtur"],'LR');
	        $this->Cell($w[5],6,$row["untush"],'LR');
	        $this->Cell($w[6],6,$row["curr"],'LR');
	        $this->Cell($w[7],6,number_format($row["debet"],2),'LR',0,'R');
	        $this->Cell($w[8],6,number_format($row["credit"],2),'LR',0,'R');
	        $this->Ln();


	        $LastNo = $row["nobkt"];
	    }
		$this->Cell(160,7,"SUB TOTAL:",1,0,'R',0);
		$this->Cell(20,7,number_format($subtotald,2),1,0,'R',0);
		$this->Cell(20,7,number_format($subtotalc,2),1,0,'R',0);
		$this->Ln();
		$this->Cell(160,7,"TOTAL:",1,0,'R',0);
		$this->Cell(20,7,number_format($totald,2),1,0,'R',0);
		$this->Cell(20,7,number_format($totalc,2),1,0,'R',0);
		$this->Ln();	    
	    //Closure line
	    //$this->Cell(array_sum($w),0,'','T');
	}

	function Footer() {
	    //Closure line
	    $this->Cell(200,0,'','T');
	}

}


?>
